"""全局配置管理模块.

提供集中化的配置管理，支持环境变量、配置文件和默认值。
"""

import os
from typing import Any, Dict, List, Optional

from pydantic import Field, validator
from pydantic_settings import BaseSettings


class DatabaseSettings(BaseSettings):
    """数据库配置."""
    
    url: str = Field(default="postgresql://user:password@localhost/netsphere")
    pool_size: int = Field(default=20)
    max_overflow: int = Field(default=30)
    pool_timeout: int = Field(default=30)
    pool_recycle: int = Field(default=3600)
    echo: bool = Field(default=False)
    
    class Config:
        env_prefix = "DB_"


class RedisSettings(BaseSettings):
    """Redis配置."""
    
    host: str = Field(default="localhost")
    port: int = Field(default=6379)
    db: int = Field(default=0)
    password: Optional[str] = Field(default=None)
    max_connections: int = Field(default=50)
    socket_timeout: int = Field(default=5)
    socket_connect_timeout: int = Field(default=5)
    
    class Config:
        env_prefix = "REDIS_"


class SecuritySettings(BaseSettings):
    """安全配置."""
    
    secret_key: str = Field(default="your-secret-key-change-this")
    algorithm: str = Field(default="HS256")
    access_token_expire_minutes: int = Field(default=30)
    api_key_header: str = Field(default="X-API-Key")
    enable_rate_limit: bool = Field(default=True)
    rate_limit: str = Field(default="100/minute")
    
    class Config:
        env_prefix = "SECURITY_"


class DHCPSettings(BaseSettings):
    """DHCP服务配置."""
    
    server_ip: str = Field(default="0.0.0.0")
    server_port: int = Field(default=67)
    lease_time: int = Field(default=3600)  # 秒
    renewal_time: int = Field(default=1800)  # 秒
    rebinding_time: int = Field(default=3150)  # 秒
    max_lease_time: int = Field(default=7200)  # 秒
    authoritative: bool = Field(default=True)
    ping_check: bool = Field(default=True)
    ping_timeout: int = Field(default=1)  # 秒
    
    class Config:
        env_prefix = "DHCP_"


class DNSSettings(BaseSettings):
    """DNS服务配置."""
    
    server_ip: str = Field(default="0.0.0.0")
    server_port: int = Field(default=53)
    upstream_servers: List[str] = Field(
        default=["8.8.8.8", "8.8.4.4", "1.1.1.1"]
    )
    cache_size: int = Field(default=10000)
    cache_ttl: int = Field(default=3600)  # 秒
    enable_dnssec: bool = Field(default=True)
    enable_logging: bool = Field(default=True)
    
    class Config:
        env_prefix = "DNS_"


class IPAMSettings(BaseSettings):
    """IPAM配置."""
    
    default_prefix_length: int = Field(default=24)
    max_prefix_length: int = Field(default=30)
    min_prefix_length: int = Field(default=8)
    enable_ip_conflict_detection: bool = Field(default=True)
    enable_automatic_cleanup: bool = Field(default=True)
    cleanup_interval: int = Field(default=3600)  # 秒
    
    class Config:
        env_prefix = "IPAM_"


class MonitoringSettings(BaseSettings):
    """监控配置."""
    
    enable_metrics: bool = Field(default=True)
    metrics_port: int = Field(default=9090)
    enable_health_checks: bool = Field(default=True)
    health_check_interval: int = Field(default=30)  # 秒
    log_level: str = Field(default="INFO")
    log_format: str = Field(default="json")
    
    class Config:
        env_prefix = "MONITORING_"


class Settings(BaseSettings):
    """全局配置类."""
    
    # 应用配置
    app_name: str = Field(default="NetSphere")
    app_version: str = Field(default="0.1.0")
    debug: bool = Field(default=False)
    host: str = Field(default="0.0.0.0")
    port: int = Field(default=8000)
    reload: bool = Field(default=False)
    
    # 子配置
    database: DatabaseSettings = Field(default_factory=DatabaseSettings)
    redis: RedisSettings = Field(default_factory=RedisSettings)
    security: SecuritySettings = Field(default_factory=SecuritySettings)
    dhcp: DHCPSettings = Field(default_factory=DHCPSettings)
    dns: DNSSettings = Field(default_factory=DNSSettings)
    ipam: IPAMSettings = Field(default_factory=IPAMSettings)
    monitoring: MonitoringSettings = Field(default_factory=MonitoringSettings)
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = False


# 全局配置实例
settings = Settings()